package utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @Description: 获取各类的路径工具类
 * @Author: syh
 * @Date: 2023/12/21/15:27
 */
public class PathUtil {
    /**
     * 获取jar路径
     * @return
     */
    public static String getJarPath(){
        String path = PathUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        try {
            return URLDecoder.decode(path,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取jar所在文件夹路径
     * @return
     */
    public static String getJarDir(){
        if(PathUtil.getJarPath().contains("jar")) {
            return PathUtil.getJarPath().replaceAll("[a-z_]+.jar", "") ;
        }else{
            return PathUtil.getJarPath();
        }
    }

    /**
     * 获取日志输出的路径
     * @return
     */
    public static String getOutLogPath(){
        return PathUtil.getJarDir() + "log.txt";
    }

    /**
     * 获取错误日志输出的路径
     * @return
     */
    public static String getErrorLogPath(){
        return PathUtil.getJarDir() + "error.txt";
    }

    /**
     * 根据传入的文件名获取对应的全路径地址
     * @param fileName
     * @return
     */
    public static String getOutFilePathByFileName(String fileName){
        return PathUtil.getJarDir() + fileName;
    }
}
